package views;

import presenters.LoginPresenter;
import com.example.wheresmystuff.R;

import data.Account;
import data.Session;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * this is the activity that allows you login in bro
 * 
 * @author Miles Raphael
 */
public class LoginActivity extends Activity implements IFieldView {

	/** presenter associated with activity */
	private LoginPresenter myPresenter;

	/**
	 * gets called when the activity is created
	 * 
	 * @param savedInstanceState
	 *            - the bundle
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		myPresenter = new LoginPresenter(this);
	}

	/**
	 * gets called when the menu is pressed
	 * 
	 * @param menu
	 *            - the menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	/**
	 * gets called when the accept button is clicked
	 * 
	 * @param source
	 *            - the view
	 */
	public void onAcceptClick(View source) {
		String userName = getUserNameField().getText().toString();
		String password = getPasswordField().getText().toString();
		myPresenter.onLoginAcceptClick(userName, password, source);
	}

	/**
	 * returns the edited text of the get username field
	 * 
	 * @return - the edittext
	 */
	private EditText getUserNameField() {
		return (EditText) findViewById(R.id.editUserName);
	}

	/**
	 * returns the edited text of the get password field
	 * 
	 * @return - the edittext
	 */
	private EditText getPasswordField() {
		return (EditText) findViewById(R.id.editPassword);
	}

	/**
	 * called when a click is clicked
	 * 
	 * @param source
	 *            - the source
	 */
	public void onRegisterClick(View source) {
		myPresenter.onLoginRegisterClick(source);
	}

	/**
	 * helps make the keyboard prettier
	 * 
	 * @param MotionEvent
	 *            ev - the motionevent
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (!isPointInsideView(ev.getRawX(), ev.getRawY(), getCurrentFocus())) {
			myPresenter.hideSoftKeyboard();
			return true;
		}
		return false;
	}

	/**
	 * @param toast
	 *            - the toast message
	 */
	@Override
	public void showToast(String toast) {
		if (toast.equals("success"))
			Toast.makeText(this, "Login Successful.", Toast.LENGTH_SHORT)
					.show();
		else if (toast.equals("failure"))
			Toast.makeText(this, "Login Failed.", Toast.LENGTH_SHORT).show();
		else if (toast.equals("locked"))
			Toast.makeText(this, "Account locked. Contact an admin.",
					Toast.LENGTH_LONG).show();
	}

	/**
	 * does nothing
	 */
	@Override
	public void showProgressBar() {
		// findViewById(R.id.progressBar).setVisibility(0);
	}

	/**
	 * checks to see if the point is inside the view
	 * 
	 * @param x
	 *            - the x position
	 * @param y
	 *            - the y position
	 * @param source
	 *            - the source
	 * @return - if the point is inside the view
	 */
	private boolean isPointInsideView(float x, float y, View source) {
		int location[] = new int[2];
		source.getLocationOnScreen(location);
		int viewX = location[0];
		int viewY = location[1];

		if ((x > viewX && x < (viewX + source.getWidth()))
				&& (y > viewY && y < (viewY + source.getHeight()))) {
			return true;
		} else
			return false;
	}

	/**
	 * hides the keyboard
	 * 
	 * @param view
	 *            - the source
	 */
	@Override
	public void hideSoftKeyboard(View source) {
		if (getCurrentFocus() != null && getCurrentFocus() instanceof EditText) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(source.getWindowToken(), 0);
		}
	}

	/**
	 * advances to the next source
	 * 
	 * @param source
	 *            - the view
	 */
	@Override
	public void advance(View source) {
		Intent intent;
		switch (source.getId()) {
		case R.id.button1:
			Account account = ((Session) getApplicationContext())
					.getAccount(this);

			if (account.getIsAdmin()) {
				intent = new Intent(this, AdminListActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(this, MainListActivity.class);
				startActivity(intent);
			}
			finish();
			break;
		case R.id.button2:
			intent = new Intent(this, CreateAccountActivity.class);
			startActivity(intent);
			break;
		}
	}
}